// MicroSD card interface
// bitrowz 2025/10/25
#pragma once
#include <stdint.h>
#include <stdbool.h>

#define Z80_SD_DATA              (0xF0)  // Data Register
#define Z80_SD_COMMAND           (0xF1)  // Command Register
#define Z80_SD_BLOCK_ADDR0       (0xF2)  // Block Address Byte-0 Register
#define Z80_SD_BLOCK_ADDR1       (0xF3)  // Block Address Byte-1 Register
#define Z80_SD_BLOCK_ADDR2       (0xF4)  // Block Address Byte-2 Register
#define Z80_SD_BLOCK_ADDR3       (0xF5)  // Block Address Byte-3 Register
#define Z80_SD_STATUS            (0xF6)  // Error/Status Register

#define COMMAND_READ_BLOCK       (0xC0)  // Read a 512-byte block
#define COMMAND_WRITE_BLOCK      (0xC1)  // Write a 512-byte block

#define ERR_OK                   (0x00)  // No error
#define ERR_READ_BLOCK           (0x70)  // Read block error
#define ERR_WRITE_BLOCK          (0x71)  // Write block error


#define SPI_CLK_DIV_0            (256) // SPI0 256x clock divider, output 400KHz, 50 KB/s
#define SPI_CLK_DIV_1            (4)   // SPI0 4x   clock divider, output 25 MHz, 12.5 MB/s

// Initialize MicroSD card module
void SD_Init();
void SD_Read(uint8_t addr, uint8_t* out);
void SD_Write(uint8_t addr, const uint8_t* in);